<?php
$server = $_SERVER['SERVER_NAME']; 
$endereco = $_SERVER ['REQUEST_URI'];
//echo "http://" . $endereco;
$enderecoCortado = explode("/", $endereco);
if(in_array("paginas", $enderecoCortado)){
Header("Location: ../login.php");
}
?>
            <div class="container-fluid">
                <div class="row-fluid">
                    <div class="span12">
                        <h3 class="page-title"> Vídeos <small>  Cadastro de vídeos</small></h3>
                        <ul class="breadcrumb">
                            <li><a href="index.php"><i class="icon-home"></i></a><span class="divider">&nbsp;</span>
                            </li>
                            <li><a href="#">Videos</a><span class="divider-last">&nbsp;</span>
                            </li>
                        </ul>
                    </div>
                </div>
<?php
        if (isset($_POST['cadastrar']) && isset($_POST['acao']) && $_POST['acao'] == 'cadastrarVideo') {
            $url = strip_tags($_POST['url']);
            
            //SE O VIDEO CADASTRADO FOR DO YOU TUBE
            if (substr_count($url, 'youtube')) {
                echo "Video do youtube!<br>";
                $player = 'youtube';

                if(substr_count($url, "https")){
                $idVideo = substr($url, 32, 11);
                }else{
                $idVideo = substr($url, 31, 11);
                }
                $thumb = 'http://i1.ytimg.com/vi/'.$idVideo.'/mqdefault.jpg';
                $conteudo = get_meta_tags('http://www.youtube.com/watch?v='.$idVideo);
                $titulo = utf8_encode($conteudo['title']);
                $descricao = utf8_encode($conteudo['description']);
            }//FIM IF VIDEO DO YOUTUBE
            else if (substr_count($url, 'vimeo')) {
                $player = 'vimeo';
                $idVideo = substr($url, 17);
                $urlImagem = parse_url($url);
                $conteudo = unserialize(file_get_contents('http://vimeo.com/api/v2/video/'.substr($urlImagem['path'], 1).'.php'));
                $thumb = $conteudo[0]['thumbnail_large'];
                $titulo = utf8_encode($conteudo[0]['title']);
                $descricao = utf8_encode($conteudo[0]['description']);
            }

            $sqlCadastrarVideo = "INSERT INTO videos (titulo, thumb, descricao, embed, player) VALUES ('$titulo','$thumb','$descricao','$idVideo','$player')";
            $cadastraVideo = mysql_query($sqlCadastrarVideo) or die(mysql_error());

            if($cadastraVideo){
                echo '<div class="alert alert-success">
                 <button data-dismiss="alert" class="close">×</button>Vídeo Cadastrado com sucesso!</div>';
            }else{
                echo '<div class="alert alert-error">
                 <button data-dismiss="alert" class="close">×</button>Ocorreu um erro ao cadastrar o vídeo!</div>';
            }

        }//FIM IF CadastraR
?>          

    <?php
    $sqlListar = "SELECT * FROM videos";
    $resultadoConsulta = mysql_query($sqlListar);
    $qtdVideosCadastrados = mysql_num_rows($resultadoConsulta);
    ?>
<div class="row-fluid">
    <div class="span8">
        <div class="widget">
        <div class="widget-title">
            <h4><i class="icon-reorder"></i> Vídeos</h4><span class="tools"><a href="javascript:;" class="icon-chevron-down"></a><a href="javascript:;" class="icon-remove"></a></span>
        </div>
<div class="widget-body">
    <form action="" method="post" enctype="multipart/form-date">
    <div class="control-group">
        <label class="control-label">Url:</label>
        <div class="controls">
        <input type="text" name="url" class="input-xxlarge"/>
        </div>
    </div>
     <div class="form-actions">
        <button class="btn btn-success" type="submit" name="cadastrar"><i class="icon-ok"></i> Salvar</button>
        <button class="btn btn-warning" type="reset"><i class=" icon-remove"></i> Limpar</button>
        <input type="hidden" name="acao" value="cadastrarVideo"/>
    </div>
    </form>
              
        </div>
        </div>                   
    </div>
    <div class="span4">
    <p>
    Cadastro de vídeos do youtube e do vimeo. Copie o link completo do vídeo para o cadastro.
    </p>
        <div class="circle-wrap">
        <div class="stats-circle turquoise-color">
        <i class="icon-facetime-video"></i>
    </div><p><strong><?php echo $qtdVideosCadastrados; ?></strong> Vídeos Cadastrados </p>
    </div>
    </div>
</div>
</div>
<div class="row-fluid">
    <div class="span12">
    <div class="widget">
        <div class="widget-title">
            <h4><i class="icon-camera-retro"></i>Ultimos Cadastrados</h4>
            <span class="tools"><a class="icon-chevron-down" href="javascript:;"></a>
                <a class="icon-remove" href="javascript:;"></a></span>
        </div>
        <div class="widget-body">
        <div class="row-fluid">
        </div>
        <div class="space10"></div>
        <div class="space10"></div>
            <div class="row-fluid">
                <?php 
                $sqlUltimosVideos = "SELECT * FROM videos ORDER BY id DESC LIMIT 6";
                $queryUltimosVideos = mysql_query($sqlUltimosVideos);
                if(mysql_num_rows($queryUltimosVideos) < 1){
                    echo 'Nenhum Vídeo cadastrado!';
                }
                while ($vid = mysql_fetch_array($queryUltimosVideos)) {
                $thumb = $vid['thumb'];
                ?>
                <div class="span2">
                   <small><?php echo utf8_decode(substr($vid['titulo'], 0, 30));
                   //echo utf8_decode($vid['titulo']);

                   ?></small>
                    <div class="thumbnail">
                        <div class="item">
                            <a href="<?php echo $thumb; ?>" title="Photo" data-rel="fancybox-button" class="fancybox-button">
                            <div class="zoom">
                                <img alt="Photo" src="<?php echo $thumb; ?>">
                            <div class="zoom-icon"></div>
                            </div>
                            </a>
                        </div>
                    </div>
                </div>
                <?php 
                }
                ?>
                <!-- FIM ROW FLUID -->
                </div>
                <div class="space10"></div>
                </div>                
            </div>
        </div>
        </div>